#include "vsprintf.h"
#include "string.h"
#include "printf.h"
#include "bsp_uart.h"


extern unsigned char getc(void);


#define	OUTBUFSIZE	1024
#define	INBUFSIZE	1024


static char g_pcOutBuf[OUTBUFSIZE];
static char g_pcInBuf[INBUFSIZE];


int printf(UART_Type* UARTx , const char *fmt, ...)
{
	int i;
	int len;
	va_list args;

	va_start(args, fmt);
	len = vsprintf(g_pcOutBuf,fmt,args);
	va_end(args);
//以下部分是作者可以魔改的部分，上面全是格式化的处理，下面才是发送函数。作者可以在此处改为自己的发送函数
	// for (i = 0; i < strlen(g_pcOutBuf); i++)
	// {
	// 	uart_write_char(UARTx , &g_pcOutBuf[i]);
	// }
	uart_write_spring(UARTx , g_pcOutBuf);
	return len;
}



int scanf(UART_Type* UARTx ,const char * fmt, ...)
{
	int i = 0;
	unsigned char state = 0;
	const char* usrchar  = fmt;
	unsigned char c;
	va_list args;
	memset(g_pcInBuf , 0 , sizeof(g_pcInBuf));
//接收部分，作者可在此部分进行魔改，加入自己的接收函数
	while(*usrchar && 0x11){
		if(*usrchar != 's'){
			usrchar++;
			continue;
		}else{
			state = 1;
			break;
		}
	}
	//清除上次在回车之后的数据，回车之后的数据表示是无效数据
	while( (((UART1->UTS)>>5) & 0x01) == 0 ){
		c = uart_read_char(UARTx);
	}
	while(1)
	{
		c = uart_read_char(UARTx);
		if((c == 0x0d) || (c == 0x0a))
		{
			g_pcInBuf[i] = '\0';
			break;
		}
		else
		{
			g_pcInBuf[i++] = c;
			

		}
	}
	if(state == 1){
		return (int)g_pcInBuf;
	}else{
		va_start(args,fmt);
		i = vsscanf(g_pcInBuf,fmt,args);
		va_end(args);
		return i;
	}	
}

